<?php

namespace app\admin\controller\system;

use app\Base;
use app\service\admin\IndexService;
use app\service\admin\system\MyInfoService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;
use think\response\View;

class MyInfo extends Base
{

    /**
     * 基本信息
     * User : Du
     * Date : 2022/11/22
     * Time : 12:10
     * @param IndexService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function show(IndexService $service): View
    {
        $res = $service::userinfo();
        return view('system/myInfo', ['data' => $res]);
    }

    /**
     * 更新基本信息
     * User : Du
     * Date : 2022/11/22
     * Time : 17:42
     * @param MyInfoService $service
     * @return Json
     * @throws DbException
     */
    public function updateInfo(MyInfoService $service): Json
    {
        $res = $service::updateData($this->params);
        if (!$res) {
            return $this->returnData('updateFail');
        }
        return $this->returnData();
    }

    /**
     * 更新密码
     * @return View
     */
    public function MyPassword(): View
    {
        return view('system/myPassword');
    }

    /**
     * 更新账号密码
     * @param MyInfoService $service
     * @return Json
     * @throws DbException
     */
    public function updatePassword(MyInfoService $service): Json
    {
        $res = $service::updatePassword($this->params);
        if (is_string($res)) {
            return $this->returnData('fail', $res);
        }
        return $this->returnData();
    }

    /**
     * 微信绑定
     * @param MyInfoService $service
     * @return View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function bindingWx(MyInfoService $service): View
    {
        $res = $service::bindInfo();
        return view('system/bindingWx', ['data' => $res]);
    }

    /**
     * 扫码绑定
     * @param MyInfoService $service
     * @return Json
     * @throws DbException
     */
    public function userBind(MyInfoService $service): Json
    {
        $res = $service::userBind();
        if (is_string($res)) {
            return $this->returnData('fail', $res);
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }

    /**
     * 绑定状态
     * @param MyInfoService $service
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function bindingStatus(MyInfoService $service): Json
    {
        $res = $service::bindingStatus($this->params);
        if (!$res) {
            return $this->returnData('fail', '未知错误');
        }
        $this->res['data'] = $res;
        return $this->returnData();
    }
}